﻿using Infrastructure.Web;
using MyData;
using MyData.Data.Repository;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRepository
{
    public class SyncErrorRepository : RepositoryBase<SyncError>, ISyncErrorRepository
    {
        public PageDto<SyncErrorPageDto> GetSyncErrors(QueryListModel query, Guid mobileId, DateTime StartDate, DateTime EndDate)
        {
            using (var db = NewDB())
            {
                var users = db.Query<UserInfo>().ToList();
                var departs = db.Query<Depart>().ToList();
                var mobiles = db.Query<Mobile>().ToList();
                if (mobileId != Guid.Empty)
                {
                    mobiles = mobiles.Where(c => c.Id == mobileId).ToList();
                }
                var visitors = db.Query<Visitor>().ToList();
                var syncErrors = db.Query<SyncError>().Where(c=>c.CreatedTime>=StartDate&&c.CreatedTime<=EndDate).ToList();
                var userError = from e in syncErrors
                                where e.UserType == (int)EnumUserType.员工
                                join m in mobiles on e.MobileId equals m.Id
                                join u in users on e.UserId equals u.Id
                                join d in departs on u.DepartId equals d.Id
                                select new SyncErrorPageDto()
                                {
                                    Id=e.Id,
                                    CreatedTime=e.CreatedTime,
                                    DepartName=d.Name,
                                    ErrorMessage=e.ErrorMessage,
                                    ErrorType=e.ErrorType,
                                    MobileId=e.MobileId,
                                    MobileName=m.Name,
                                    NickName=u.NickName,
                                    OperateType=e.OperateType,
                                    Status=e.Status,
                                    UserId=e.UserId,
                                    UserType=e.UserType
                                };
                var visiterError= from e in syncErrors
                                  where e.UserType == (int)EnumUserType.访客
                                  join m in mobiles on e.MobileId equals m.Id
                                  join v in visitors on e.UserId equals v.Id
                                  select new SyncErrorPageDto()
                                  {
                                      Id = e.Id,
                                      CreatedTime = e.CreatedTime,
                                      ErrorMessage = e.ErrorMessage,
                                      ErrorType = e.ErrorType,
                                      MobileId = e.MobileId,
                                      MobileName = m.Name,
                                      NickName = v.Name,
                                      OperateType = e.OperateType,
                                      Status = e.Status,
                                      UserId = e.UserId,
                                      UserType = e.UserType
                                  };
                var result = userError.Concat(visiterError).AsQueryable().OrderByDescending(c=>c.CreatedTime).Paging(query.Limit, query.Offset);
                return new PageDto<SyncErrorPageDto>()
                {
                    total = result.total,
                    rows = result.rows
                };

            }
        }

        public bool SaveSyncError(SyncError syncError)
        {
            using (var db = NewDB())
            {
                syncError.Id = Guid.NewGuid();
                db.Insert<SyncError>(syncError);
                return true;
            }
        }
        public bool SyncErrorBatchDelete(List<Guid> Ids)
        {
            using (var db = NewDB())
            {
                return db.Delete<SyncError>(c => Ids.Contains(c.Id)) > 0;
            }
        }
        public bool SyncErrorRepeate(Guid Id)
        {
            using (var db = NewDB())
            {
                var error = db.QueryByKey<SyncError>(Id);
                db.Insert<SyncRL>(new SyncRL() { Id=Guid.NewGuid(),OperateType=error.OperateType,MobileId=error.MobileId,UserId=error.UserId,UserType=error.UserType});
                return true;
            }

        }
    }
}
